package com.cz.run.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.run.beans.base.PageDTO;
import com.cz.run.beans.base.ReturnDTO;
import com.cz.run.beans.mysql.News;
import com.cz.run.beans.po.news.InfoPO;
import com.cz.run.beans.po.news.ListPO;
import com.cz.run.beans.vo.news.NewsInfo;
import com.cz.run.beans.vo.news.NewsVO;
import com.cz.run.service.INewsService;
import com.cz.run.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO()
 * @auther:CAIYJ
 * @Date:2018/5/23 22:29
 */
@Api(value = "新闻")
@RestController
@RequestMapping(value = "/news")
public class NewsServer {

    @Autowired
    private INewsService newsService;

    @ApiOperation(value = "新闻列表", notes = "新闻列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/list")
    public ReturnDTO<PageDTO<NewsVO>> list(@RequestBody ListPO po) throws Exception {
        Page<News> page = new Page<>(po.getCurrentPage(), po.getPageSize());
        EntityWrapper<News> ew = new EntityWrapper<>();
        ew.where(po.getType() != null, "type = {0}", po.getType());
        page = newsService.selectPage(page, ew);
        if (page != null && page.getRecords() != null && page.getRecords().size() > 0) {
            List<NewsVO> list = new ArrayList<>();
            NewsVO vo = null;
            for (News news : page.getRecords()) {
                vo = new NewsVO();
                BeanUtils.copyProperties(news, vo);
                if (null != news.getCreateTime()) {
                    vo.setTime(DateUtil.currentTimeMillis4(news.getCreateTime()));
                }
                list.add(vo);
            }
            po.setContent(list);
        }
        po.setTotalRecord(page.getTotal());
        return ReturnDTO.success(po);
    }

    @ApiOperation(value = "新闻详情", notes = "新闻详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping(value = "/info")
    public ReturnDTO<NewsInfo> info(@RequestBody InfoPO po) throws Exception {
        News news = newsService.selectById(po.getId());
        NewsInfo info = new NewsInfo();
        BeanUtils.copyProperties(news, info);
        if (news != null && news.getCreateTime() != null) {
            info.setCreateTimeStr(DateUtil.currentTimeMillisCN1(news.getCreateTime()));
        }
        return ReturnDTO.success(info);
    }
}
